package com.miot.domain.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.miot.object.skill.SkillBean;
import org.apache.http.NameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: zhangyanbin
 * @Date: 2019/1/27 18:19
 * @Version 1.0
 */
public class MISkillUtil {
    protected static Logger logger = LoggerFactory.getLogger(MISkillUtil.class);

    /**
     * 增加Skill POST
     */
    public static final String MI_CREATE_SKILL = "/bridge/s2s/skill/add";
    /**
     * 获取Skill GET
     */
    public static final String MI_GET_SKILL = "/bridge/s2s/skill/get";
    /**
     * 删除Skill GET
     */
    public static final String MI_DELETE_SKILL = "/bridge/s2s/skill/delete";

    /**
     * 获取技能列表 GET
     */
    public static final String MI_DEV_SKILL_LIST = "/bridge/s2s/skill/devskilllist";
    /**
     * 获取技能列表 GET
     */
    public static final String MI_GET_SKILL_LIST = "/bridge/s2s/skill/getlist";

    /**
     * 获取技能列表
     *
     * @param controlUnitId
     * @return
     */
    public static JSONArray getDevSkillList(long controlUnitId) {
        try {
            String url = ControlUnitUtil.URI + MISkillUtil.MI_DEV_SKILL_LIST;
            String uri = MISkillUtil.MI_DEV_SKILL_LIST;
            return ControlUnitUtil.getListByControlUnitId(controlUnitId, url, uri, "getDevSkillList");
        } catch (Exception e) {
            logger.error("接口tvLogin：获取设备列表错误信息是->", e.getMessage());
        }
        return new JSONArray();
    }

    /**
     * 增加skill
     *
     * @param controlUnitId
     * @param skills
     * @return
     */
    public static JSONArray createSkill(Long controlUnitId, SkillBean skills) {
        try {
            String url = ControlUnitUtil.URI + MISkillUtil.MI_CREATE_SKILL;
            JSONObject dataValueJson = new JSONObject();
            dataValueJson.put("controlUnitId", controlUnitId);
            dataValueJson.put("compact", false);
            dataValueJson.put("skills", new Gson().toJson(skills));
            List<NameValuePair> params = ControlUnitUtil.createParams(ControlUnitUtil.POST_METHOD, MISkillUtil.MI_CREATE_SKILL, dataValueJson.toString());
            String postResult = HttpUtils.sendPost(url, params);
            JSONObject resultJSON = JSONObject.parseObject(postResult);
            if (StringUtils.pathEquals("ok", resultJSON.getString("result"))) {
                JSONArray responseBody = JSONObject.parseObject(postResult).getJSONObject("data").getJSONArray("resultIds");
                return responseBody;
            } else {
                logger.error("小米接口->增加skill接口={}", resultJSON);
                return new JSONArray();
            }
        } catch (Exception e) {
            logger.error("小米接口：增加skill错误信息是->", e.getMessage());
        }
        return new JSONArray();
    }

    /**
     * 删除skill
     *
     * @param controlUnitId
     * @param skillId
     * @return
     */
    public static JSONObject deleteSkill(Long controlUnitId, Long skillId) {
        try {
            String url = ControlUnitUtil.URI + MISkillUtil.MI_DELETE_SKILL;
            JSONObject dataValueJson = new JSONObject();
            dataValueJson.put("controlUnitId", controlUnitId);
            dataValueJson.put("skillId", skillId);
            List<NameValuePair> params = ControlUnitUtil.createParams(ControlUnitUtil.POST_METHOD, MISkillUtil.MI_DELETE_SKILL, dataValueJson.toString());
            String postResult = HttpUtils.sendPost(url, params);
            JSONObject resultJSON = JSONObject.parseObject(postResult);
            if (StringUtils.pathEquals("ok", resultJSON.getString("result"))) {
                JSONObject data = JSONObject.parseObject(postResult).getJSONObject("data");
                return data;
            } else {
                logger.error("小米接口->删除skill接口={}", resultJSON);
                return new JSONObject();
            }
        } catch (Exception e) {
            logger.error("小米接口：删除skill错误信息是->", e.getMessage());
        }
        return new JSONObject();
    }

    /**
     * 获取skill
     *
     * @param controlUnitId
     * @param keyWord
     * @param lastSkillId
     * @param limit
     * @return
     */
    public static JSONArray getSkill(Long controlUnitId, String keyWord, Long lastSkillId, Long limit) {
        try {
            String url = ControlUnitUtil.URI + MISkillUtil.MI_GET_SKILL;
            JSONObject dataValueJson = new JSONObject();
            dataValueJson.put("controlUnitId", controlUnitId);
            dataValueJson.put("keyWord", keyWord);
            dataValueJson.put("lastSkillId", lastSkillId);
            dataValueJson.put("limit", limit);
            List<NameValuePair> params = ControlUnitUtil.createParams(ControlUnitUtil.GET_METHOD, MISkillUtil.MI_GET_SKILL, dataValueJson.toString());
            String postResult = HttpUtils.sendGet(url, params,null);
            JSONObject resultJSON = JSONObject.parseObject(postResult);
            if (StringUtils.pathEquals("ok", resultJSON.getString("result"))) {
                JSONArray responseBody = JSONObject.parseObject(postResult).getJSONObject("data").getJSONArray("resultIds");
                return responseBody;
            } else {
                logger.error("小米接口->获取skill接口={}", resultJSON);
                return new JSONArray();
            }
        } catch (Exception e) {
            logger.error("小米接口：获取skill错误信息是->", e.getMessage());
        }
        return new JSONArray();
    }
}
